package cn.iocoder.yudao.module.iot.controller;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.iot.controller.admin.devicedataupload.vo.DeviceDataUploadPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.devicedataupload.vo.DeviceDataUploadRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.devicerel.vo.DeviceRelPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.devicerel.vo.DeviceRelRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.devicerel.vo.DeviceRelSaveReqVO;
import cn.iocoder.yudao.module.iot.convert.devicedataupload.DeviceDataUploadConvert;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicedataupload.DeviceDataUploadDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicerel.DeviceRelDO;
import cn.iocoder.yudao.module.iot.service.deviceeventslog.DeviceEventsLogService;
import cn.iocoder.yudao.module.iot.service.devicerel.DeviceRelService;
import cn.iocoder.yudao.module.iot.util.AliyunIotApiUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController
@RequestMapping("/admin-api/deviceManage")
@Tag(name = "设备管理")
@Validated
public class IotDeviceManageController {

    private final static Logger logger = LoggerFactory.getLogger(IotDeviceManageController.class);


    @PostMapping("/getProductPage")
    @Operation(summary = "获得产品列表分页")
    public Object getProductPage(@RequestParam("pageNo") int pageNo,
                                 @RequestParam("pageSize") int pageSize) {
        Object productInfoList = AliyunIotApiUtil.getProductInfoList(pageNo, pageSize);
        return success(productInfoList);
    }

    @PostMapping("/getProductDetail")
    @Operation(summary = "获得产品详情")
    public Object getProductDetail(@RequestParam("productKey") String productKey) {
        Object productInfo = AliyunIotApiUtil.getProductInfo(productKey);
        return success(productInfo);
    }


    @PostMapping("/getDevicePage")
    @Operation(summary = "分页查询指定产品下的设备列表")
    public Object getDevicePage(@RequestParam("productKey") String productKey,
                                @RequestParam("pageNo") int pageNo,
                                @RequestParam("pageSize") int pageSize,
                                @RequestParam("nextToken") String nextToken) {
        Object list = AliyunIotApiUtil.getDeviceListByProductKey(productKey, pageNo, pageSize,nextToken);
        return success(list);
    }

    @PostMapping("/queryDeviceByStatus")
    @Operation(summary = "按状态查询设备列表")
    public Object queryDeviceByStatus(@RequestParam("status") Integer status,
                                @RequestParam("pageNo") int pageNo,
                                @RequestParam("pageSize") int pageSize,
                                @RequestParam("productKey") String productKey) {
        Object list = AliyunIotApiUtil.queryDeviceByStatus(status,productKey,pageNo,pageSize);
        return success(list);
    }

    @PostMapping("/queryDeviceStatistics")
    @Operation(summary = "统计设备不同状态下的数据")
    public Object queryDeviceStatistics(@RequestParam("productKey") String productKey) {
        Object list = AliyunIotApiUtil.queryDeviceStatistics(productKey);
        return success(list);
    }


    @PostMapping("/queryDeviceDetail")
    @Operation(summary = "查询设备详情")
    public Object queryDeviceDetail(@RequestParam("productKey") String productKey,
                                    @RequestParam("deviceName") String deviceName) {
        Object list = AliyunIotApiUtil.queryDeviceDetail(productKey,deviceName);
        return success(list);
    }

}
